package controllers

import (
	"baokaobang/datasourse"
	"baokaobang/models"
	"baokaobang/utils"
	"baokaobang/wechat"
	"fmt"
	"strings"

	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/sessions"
)

type Comment struct {
	Ctx iris.Context
}

// 给某条说说发表评论
func (c *Comment) PostCreate() interface{} {
	channel := c.Ctx.PostValue("channel")
	trendsId := utils.Atoi64(c.Ctx.PostValueDefault("trendsId", "0"))
	toId := utils.Atoi64(c.Ctx.PostValueDefault("toId", "0"))
	content := strings.TrimSpace(c.Ctx.PostValueDefault("content", ""))

	if len(channel) <= 0 {
		return utils.CommonRes(1000, "请提供渠道", "")
	}
	if trendsId <= 0 {
		return utils.CommonRes(1000, "请提供说说id", "")
	}
	if len(content) <= 0 {
		return utils.CommonRes(1000, "请输入评论内容", "")
	}

	mType := 1
	if toId > 0 {
		mType = 2
	}

	msgSec := wechat.NewMsgSec(content)
	msgSecRes, err := msgSec.Check()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("请求异常：%s", err.Error()), "")
	}
	if !msgSecRes {
		return utils.CommonRes(1000, "请不要发表敏感内容", "")
	}

	var userId int64
	if strings.EqualFold(channel, "wechat") {
		token := c.Ctx.GetHeader("token")
		claims, err := utils.ParseToken(token)
		if err != nil {
			return utils.CommonRes(1000, fmt.Sprintf("解析token出错：%s", err.Error()), "")
		}
		userId = utils.Atoi64(claims.Id)
	} else {
		session := sessions.Get(c.Ctx)
		userId, _ = session.GetInt64(fmt.Sprintf("%sUserId", channel))
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	comment := new(models.Comment)
	comment.UserId = userId
	comment.ToId = toId
	comment.TrendsId = trendsId
	comment.Content = content
	comment.MType = mType
	_, err = db.InsertOne(comment)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("评论失败：%s", err.Error()), "")
	}

	trends := new(models.Trends)
	_, err = db.ID(trendsId).Get(trends)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("评论失败：%s", err.Error()), "")
	}

	updateData := map[string]interface{}{
		"comment_num": trends.CommentNum + 1,
	}
	db.Table(new(models.Trends)).ID(trendsId).Update(updateData)

	newComment := new(models.CommentObj)
	_, err = db.Table(new(models.Comment)).Alias("c").
		Join("LEFT", []string{"user", "fu"}, "c.user_id = fu.id").
		Join("LEFT", []string{"user", "tu"}, "c.to_id = tu.id").
		Where("c.id = ?", comment.Id).Get(newComment)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("评论失败：%s", err.Error()), "")
	}

	return utils.CommonRes(20000, "评论成功", newComment)
}

//评论和点赞记录
func (c *Comment) PostList() interface{} {
	userType := c.Ctx.PostValue("type")
	channel := c.Ctx.PostValue("channel")

	if userType == "" || channel == "" {
		return utils.CommonRes(1000, fmt.Sprintf("参数有误"), "")
	}

	var userId int64
	if channel == "wechat" {
		token := c.Ctx.GetHeader("token")
		if token == "" {
			return utils.CommonRes(1000, fmt.Sprintf("token为空"), "")
		}

		claims, err := utils.ParseToken(token)
		if err != nil {
			return utils.CommonRes(1000, fmt.Sprintf("解析token出错"), "")
		}

		userId = utils.Atoi64(claims.Id)
	} else {
		session := sessions.Get(c.Ctx)
		userId, _ = session.GetInt64(fmt.Sprintf("%s userId", channel))
	}

	//连接数据库
	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	var commentList []models.CommentObj
	var thumbsUpList []models.ThumbsUpObj

	if userType == "2" {
		err := db.Table(new(models.Trends)).Where("user_id = ?", userId).Desc("created").Find(&commentList)
		if err != nil {
			return utils.CommonRes(1000, fmt.Sprintf("查询评论数据出错: %s", err), "")
		}

		err = db.Table(new(models.Trends)).Where("user_id = ?", userId).Desc("created").Find(&thumbsUpList)
		if err != nil {
			return utils.CommonRes(1000, fmt.Sprintf("查询点赞数据出错: %s", err), "")
		}
	}

	data := map[string]interface{}{
		"commentList":  commentList,
		"thumbsUpList": thumbsUpList,
	}

	return utils.CommonRes(20000, fmt.Sprintf("获取评论历史记录成功"), data)
}
